﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using WindowsGame.Enums;

namespace WindowsGame.Input
{
	public class XboxInputFactory : IInputFactory
	{
		private GamePadState currGamePadState;
		private GamePadState prevGamePadState;

		public void Update(GameTime gameTime)
		{
			prevGamePadState = currGamePadState;
			currGamePadState = GamePad.GetState(PlayerIndex.One); 
		}

		public Boolean Escape()
		{
			return ButtonPress(Buttons.Back);
		}
		public Boolean Load()
		{
			return false;
		}
		public Direction Rotate()
		{
			if (currGamePadState.ThumbSticks.Left.X < 0 || ButtonPress(Buttons.DPadLeft))
			{
				return Direction.Left;
			}
			if (currGamePadState.ThumbSticks.Left.X > 0 || ButtonPress(Buttons.DPadRight))
			{
				return Direction.Right;
			}

			return Direction.None;
		}
		public Boolean Thrust()
		{
			return currGamePadState.Triggers.Right > 0;
		}
		public Boolean Brake()
		{
			return currGamePadState.Triggers.Left > 0;
		}
		public Boolean Shoot()
		{
			return ButtonPress(Buttons.A);
		}
		public Boolean Next()
		{
			return ButtonPress(Buttons.X);
		}
		public Boolean Prev()
		{
			return ButtonPress(Buttons.B);
		}

		private Boolean ButtonPress(Buttons button)
		{
			return currGamePadState.IsButtonDown(button);
		}
		private Boolean ButtonHold(Buttons button)
		{
			return currGamePadState.IsButtonDown(button) && prevGamePadState.IsButtonUp(button);
		}
	}
}